<template>
  <div
    class="ky-container schedule-container"
    :style="{height: qktVisible?'calc(100vh - 250px)': 'calc(100vh - 50px)'}"
    @click="scheduleDlgClose"
  >
    <el-card shadow="hover" class="ky-card-xs ky-card-hdbg">
      <el-row>
        <el-col :span="16">
          <el-form
            :inline="true"
            :model="formdata"
            @submit.native.prevent
            class="ky-form-inline ky-form-inline-sm">
            <el-form-item label="医院机房">
              <roomCascader :room.sync="formdata.room" :refreshLocal="true" @roomChange="roomChange"></roomCascader>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="8">
          <div style="text-align: right;line-height: 46px;">
            <el-button size="small" @click="viewChange">{{viewType===0?'机房号源管理':'机房号源模板设置'}}</el-button>
            <span class="releaseauto"><i class="dot" :class="{isauto: checkroomModelObj.isAutoPublish}"></i>{{checkroomModelObj.isAutoPublish?'自动发布中':'未自动发布'}}</span>
            <!-- <i style="margin-left: 6px;" class="el-icon-arrow-down"></i> -->
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-card shadow="hover" class="ky-card-xs ky-card-hdbg ky-mt10" v-show="formdata.room.length>0">
      <template>
        <tempView v-show="viewType===0" ref="tempView"></tempView>
      </template>
      <template>
        <scheduleView v-show="viewType===1" ref="scheduleView" @updateTemplateSuccesss="updateTemplateSuccesss"></scheduleView>
      </template>
    </el-card>
    <roomNone v-show="formdata.room.length===0"></roomNone>
    <quicktemplate
      ref="quicktemplate"
      :qt-show.sync="qktVisible"
      :checked-room="formdata.room"
      @quickTempChange="quickTempChange"
    ></quicktemplate>
  </div>
</template>

<script>
import { setLocalStorage, getLocalStorage } from '@/utils/localStorage'
import { getCheckroomModelAutoPublish } from '@/api/room'
import quicktemplate from '../components/quickTemplate'
import roomCascader from '../components/roomCascader'
import tempView from './tempView'
import scheduleView from './scheduleView'
import roomNone from '@/components/roomNone'
export default {
  components: { roomCascader, tempView, scheduleView, quicktemplate, roomNone },
  provide() {
    return {
      tempListUpdate: this.tempListUpdate
    }
  },
  data() {
    return {
      checkRoom: [],
      formdata: {
        room: []
      },
      viewType: 1, // 0:模板视图，1：排班视图
      viewTypeNum: 0,

      checkroomModelObj: {
        id: 0, // 排班模板id
        isAutoPublish: 0, // 是否自动发布
        publishWeeks: 0, // 自动发布几周
        isIncludedHolidays: false // 是否包含节假日
      },

      // 快捷选择模板
      quickTempObj: null,
      qktVisible: false,
      hospCode: ''
    }
  },
  created() {
    this.formdata.room = this.$store.state.user.checkRoom
  },
  mounted() {
    this.roomChange(this.formdata.room)
    // // console.log('===this.formdata.room===', this.formdata.room)
    this.$refs.quicktemplate.checkRoomInit(this.formdata.room)
  },
  methods: {
    changeHospitalCode(data) {
      this.hospCode = data
    },
    // 机房改变
    roomChange(roomData) {
      console.warn('roomChange=>roomData', roomData)
      // // console.log('这里动态更新this.parametersTypeByPart')
      if (roomData) {
        this.getCheckroomModelPost()
      }
      if (this.viewType === 0) {
        this.$refs.tempView.init(this.formdata.room)
      } else {
        this.$refs.scheduleView.init(this.formdata.room)
      }
    },
    viewChange() {
      // console.log('viewChange========================')
      this.viewType = this.viewType === 0 ? 1 : 0
      setLocalStorage('jjyy-viewType', this.viewType)
      this.roomChange()
      this.$nextTick(() => {
        // 视图切换时，收起快速选择模板
        this.qktVisible = false
        this.quickTempObj = null
        this.quickTempChange()
      })
    },
    // 快捷模板选择监听
    quickTempChange(quickTempObj) {
      this.quickTempObj = quickTempObj ? { ...quickTempObj } : null
      // // console.log('==lqt==viewType', this.viewType)
      if (this.viewType === 0) {
        this.$refs.tempView.quickTempChange(this.quickTempObj)
      } else {
        this.$refs.scheduleView.quickTempChange(this.quickTempObj)
      }
    },
    // 自动发布，机房对象数据获取
    async getCheckroomModelPost() {
      // // console.log('didi')
      if (this.formdata.room.length === 0) return
      const params = {
        HospitalCode: this.formdata.room[0] || '',
        DepArea: this.formdata.room[1] || '',
        Dep: this.formdata.room[2] || '',
        ModalityType: this.formdata.room[3] || '',
        CheckRoom: this.formdata.room[4] || ''
      }
      console.log('getCheckroomModelPost=>getCheckroomModelPost==>params', params)
      const roomModel = await getCheckroomModelAutoPublish(params)
      this.checkroomModelObj = { ...roomModel }
    },
    // 视图更新模板
    updateTemplateSuccesss() {
      // console.log(1234)
      this.$refs.tempView.init(this.formdata.room, true)
    },
    tempListUpdate() {
      this.$refs.quicktemplate && this.$refs.quicktemplate.getTempList()
    },
    // 关闭弹框
    scheduleDlgClose() {
      if (this.viewType === 0) {
        this.$refs.tempView && this.$refs.tempView.dlgClose()
      } else {
        this.$refs.scheduleView && this.$refs.scheduleView.dlgClose()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.schedule-container{
  height: calc(100vh - 50px);
  overflow-y: auto;
}
.releaseauto{
  margin:0 20px;
  cursor: pointer;
  font-size: 14px;
  .dot{
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 8px;
    background-color: #EB5353;
  }
  .isauto{
    background-color: rgb(67, 207, 32);
  }
}
</style>
